<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/12
 * Time: 下午11:39
 */

namespace Home\Model;
use Think\Model;

class MissionModel extends Model {
    protected $tableName = 'mission';
    protected $pk        = 'mission_id';

    //根据查询条件获取接活首页信息-未登陆
    public function getMInfo($jobs,$duration){
        //根据查询条件获取任务基本信息
        $mission_field = array('mission_id','mission_name','jobs','salary1','salary2','location1','location2','publish_time','publisher_id','duration');
        $mission_order = '`publish_time` desc';
        $job_arr = explode(",",$jobs);
        for($i=0;$i < count($job_arr);$i++){
            $job_arr[$i] = '%'.$job_arr[$i].'%';
        }
        $duration_arr = explode(",",$duration);
        for($i=0;$i < count($duration_arr);$i++){
            $duration_arr[$i] = '%'.$duration_arr[$i].'%';
        }
        $mission_map['jobs'] = array('like',$job_arr,'or');
        $mission_map['duration'] = array('like',$duration_arr,'or');
        $mission_map['status'] = array('eq',1);
        //dump($user_ext_map);
        $mission_result = $this->table('Mission')->field($mission_field)->order($mission_order)->where($mission_map)->select();
        //dump($mission_result);
        if(is_array($mission_result)){
            foreach($mission_result as &$mission_result_one){
                $user_ext_field = array('level_j');
                $user_ext_map['user_id'] = array('eq',$mission_result_one['publisher_id']);
                //dump($user_ext_map);
                $user_ext_info = $this->table('User_ext')->field($user_ext_field)->where($user_ext_map)->select();
                //dump($user_ext_info);
                if(is_array($user_ext_info)){
                    $mission_result_one['level_j'] = $user_ext_info[0]['level_j'];
                }else{
                    //user_ext获取甲方等级失败
                }
            }
        }else{
            //mission表获取数据失败
            //return 0;
        }
        //dump($mission_result);
        return $mission_result;

    }

    //根据查询条件获取接活首页信息－登陆
    public function getMInfoLogin($keyword,$jobs,$duration){
        $mission_field = array('mission_id','mission_name','jobs','salary1','salary2','location1','location2','publish_time','publisher_id','publisher_name','duration','agent');
        $mission_order = '`publish_time` desc';
        //关键字查询
        $mission_map_sub['publisher_name'] = array('like','%'.$keyword.'%');
        $mission_map_sub['mission_name'] = array('like','%'.$keyword.'%');
        $mission_map_sub['agent'] = array('like','%'.$keyword.'%');
        $mission_map_sub['location2'] = array('like','%'.$keyword.'%');
        $mission_map_sub['salary2'] = array('like','%'.$keyword.'%');
        $mission_map_sub['_logic'] = 'or';
        $mission_map['_complex'] = $mission_map_sub;

        $job_arr = explode(",",$jobs);
        for($i=0;$i < count($job_arr);$i++){
            $job_arr[$i] = '%'.$job_arr[$i].'%';
        }
        $duration_arr = explode(",",$duration);
        for($i=0;$i < count($duration_arr);$i++){
            $duration_arr[$i] = '%'.$duration_arr[$i].'%';
        }
        $mission_map['jobs'] = array('like',$job_arr,'or');
        $mission_map['duration'] = array('like',$duration_arr,'or');
        $mission_map['status'] = array('eq',1);
        //dump($mission_map);
        $mission_result = $this->table('Mission')->field($mission_field)->order($mission_order)->where($mission_map)->select();
        //dump($mission_result);
        if(is_array($mission_result)){
            foreach($mission_result as &$mission_result_one){
                $user_ext_field = array('level_j');
                $user_ext_map['user_id'] = array('eq',$mission_result_one['publisher_id']);
                //dump($user_ext_map);
                $user_ext_info = $this->table('User_ext')->field($user_ext_field)->where($user_ext_map)->select();
                //dump($user_ext_info);
                if(is_array($user_ext_info)){
                    $mission_result_one['level_j'] = $user_ext_info[0]['level_j'];
                }else{
                    //user_ext获取甲方等级失败
                }
            }
        }else{
            //mission表获取数据失败
            //return 0;
        }
        //dump($mission_result);
        return $mission_result;
    }

} 